/*******************************************************************************
*   				               DOFILE: BALANCE TABLES					                       *
********************************************************************************

    **  OUTLINE:

        1. Balance tables
            - Define variables for balance tables
            - Baseline only
            - Endline only
            - Baseline dependent variables

    **  NOTES:

        - Tables are generated in csv format and need to be imported into excel

********************************************************************************/


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  * Load dataset
  use "$Endline_dtFin/data_angolacbm.dta", clear

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* 1. Balance tables

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// Define variables for balance tables

  // School

  global balance_S ///
    teachers classrooms students schoolage has79 distance_mc urban scorecard_z_1s

  // Parents

  global balance_P ///
    P_female P_age P_primedu P_children P_pt P_social  ///
    P_student_female P_student_age

  // Teachers

  global balance_T ///
    T_female T_age T_highedu T_children T_pt T_social

  // Directors

  global balance_D ///
    D_female D_age D_highedu D_children D_pt D_social

  // All stakeholders

  global balance_vars ///
      $balance_S $balance_P $balance_T $balance_D

  // Baseline variables related to parents mobility and school management

  global basebal ///
    basebal_PQ6_12 basebal_PQ6_13 basebal_PQ3_12 basebal_PQ2_12 ///
    basebal_TD7base_14 basebal_parmeet basebal_TD3base_17       ///
    basebal_boardpar basebal_PQ9_09 basebal_PQ4_16              ///
    basebal_PQ7_07 basebal_publicinfo basebal_onF basebal_con   ///
    basebal_bath basebal_water basebal_desk basebal_chairs      ///
    basebal_excbooks

// Tables 1 and 2 (Baseline)

  // Balance Tests

  foreach var in $balance_P{
  qui: reg `var' T1 T2 T3 if t==0, cluster(school_id)
  eststo `var'
  qui: test T1 T2 T3
  estadd scalar joint_all = r(p), replace
  qui: test T1=T2
  estadd scalar inf_col = r(p), replace
  qui: test T1=T3
  estadd scalar inf_duo = r(p), replace
  qui: test T2=T3
  estadd scalar col_duo = r(p), replace
  }

  foreach var in $balance_T{
  qui: reg `var' T1 T2 T3 if t==0, cluster(school_id)
  eststo `var'
  qui: test T1 T2 T3
  estadd scalar joint_all = r(p), replace
  qui: test T1=T2
  estadd scalar inf_col = r(p), replace
  qui: test T1=T3
  estadd scalar inf_duo = r(p), replace
  qui: test T2=T3
  estadd scalar col_duo = r(p), replace
  }

  foreach var in $balance_D $balance_S{
  qui: reg `var' T1 T2 T3 if t==0 & regschool==1
  eststo `var'
  qui: test T1 T2 T3
  estadd scalar joint_all = r(p), replace
  qui: test T1=T2
  estadd scalar inf_col = r(p), replace
  qui: test T1=T3
  estadd scalar inf_duo = r(p), replace
  qui: test T2=T3
  estadd scalar col_duo = r(p), replace
  }

  // Generate table

  esttab $balance_vars ///
    using "$Endline_outRaw/table1-2.csv", replace ///
  	label b(3) se ///
    keep(T1 T2 T3 _cons) ///
    order(_cons T1 T2 T3) ///
  	wide ///
    s(joint_all inf_col inf_duo col_duo N, ///
      label(  "T1 = T2 = T3 = 0 (F p-val)" ///
              "T1 = T2 (F p-val)" ///
              "T1 = T3 (F p-val)" ///
              "T2 = T3 (F p-val)" ///
              "Observations")) ///
  	title(Baseline) ///
    starl(* 0.10 ** 0.05 *** 0.01) ///
    nonotes


// Tables A5 and A6 (Endline)

  // Tests

  foreach var in $balance_P{
  qui: reg `var' T1 T2 T3 if t==1 & regparent==1, cluster(school_id)
  eststo `var'
  qui: test T1 T2 T3
  estadd scalar joint_all = r(p), replace
  qui: test T1=T2
  estadd scalar inf_col = r(p), replace
  qui: test T1=T3
  estadd scalar inf_duo = r(p), replace
  qui: test T2=T3
  estadd scalar col_duo = r(p), replace
  }

  foreach var in $balance_T{
  qui: reg `var' T1 T2 T3 if t==1 & regteacher==1, cluster(school_id)
  eststo `var'
  qui: test T1 T2 T3
  estadd scalar joint_all = r(p), replace
  qui: test T1=T2
  estadd scalar inf_col = r(p), replace
  qui: test T1=T3
  estadd scalar inf_duo = r(p), replace
  qui: test T2=T3
  estadd scalar col_duo = r(p), replace
  }

  foreach var in $balance_D $balance_S{
  qui: reg `var' T1 T2 T3 if t==1 & regschool==1
  eststo `var'
  qui: test T1 T2 T3
  estadd scalar joint_all = r(p), replace
  qui: test T1=T2
  estadd scalar inf_col = r(p), replace
  qui: test T1=T3
  estadd scalar inf_duo = r(p), replace
  qui: test T2=T3
  estadd scalar col_duo = r(p), replace
  }

  // Generate table

  esttab $balance_vars ///
    using "$Endline_outRaw/tableA5-A6.csv", replace ///
  	label b(3) se ///
    keep(T1 T2 T3 _cons) ///
    order(_cons T1 T2 T3) ///
  	wide ///
    s(joint_all inf_col inf_duo col_duo N, ///
      label(  "T1 = T2 = T3 = 0 (F p-val)" ///
              "T1 = T2 (F p-val)" ///
              "T1 = T3 (F p-val)" ///
              "T2 = T3 (F p-val)" ///
              "Observations")) ///
  	title(Endline) ///
    starl(* 0.10 ** 0.05 *** 0.01) ///
    nonotes

// Table A4 (Baseline balance in parents' mobility and school managment variables)

  // Tests

  foreach var in $basebal{
    qui: reg `var' T1 T2 T3, cluster(school_id)
    eststo `var'
    qui: test T1 T2 T3
    estadd scalar joint_all = r(p), replace
    qui: test T1=T2
    estadd scalar inf_col = r(p), replace
    qui: test T1=T3
    estadd scalar inf_duo = r(p), replace
    qui: test T2=T3
    estadd scalar col_duo = r(p), replace
  }

  // Generate table

  esttab $basebal ///
    using "$Endline_outRaw/tableA4.csv", replace ///
    label b(3) se ///
    keep(T1 T2 T3 _cons) ///
    order(_cons T1 T2 T3) ///
    wide ///
    s(joint_all inf_col inf_duo col_duo N, ///
      label(  "T1 = T2 = T3 = 0 (F p-val)" ///
              "T1 = T2 (F p-val)" ///
              "T1 = T3 (F p-val)" ///
              "T2 = T3 (F p-val)" ///
              "Observations")) ///
    title(Basebal) ///
    starl(* 0.10 ** 0.05 *** 0.01) ///
    nonotes


****** End of dofile
